<?php
	require '../include/library.inc.php';
	require '../include/admin_login.inc.php';

$course_id = intval(($_GET['course_id'])?$_GET['course_id']:$_POST['course_id']);
if (!$course_id) redirect("schools.php");
	
# Ако запазваме	
if ($_POST['is_submitted']) {
    $error_message = '';
    
	# Проверки
	if (!$_POST['duration'])  $error_message .= 'Duration is required<br/>';	
    
	# OK
    if(!$error_message) {
				
		# Create the course
		$sql = 'INSERT INTO course_dates
				  (course_id, start_date, duration, tuition)
				VALUES
				  ('.$course_id.', \''.escape($_POST['start_date']).'\', '.intval($_POST['duration']).', '.floatval($_POST['tuition']).')
				  ';
		query($sql);
	  			
		redirect("course_dates.php?course_id=".$course_id);
	  
    }
	
}

$sql = 'SELECT
		  name
		FROM
		  courses
		WHERE
		  course_id = '.$course_id.'
	';
$row_course = mysql_fetch_assoc(query($sql));


head_admin("Create New Course");
?>
<script type="text/javascript" src="<?=HTML_BASE_ADMIN?>js/calendarDateInput.js"></script>
<script type="text/javascript" src="<?=HTML_BASE_ADMIN?>tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({
	// General options
	mode : "exact",
    elements : "body",
	theme : "advanced",
	relative_urls : false,
	plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,imagemanager",

	// Theme options
	theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect",
	theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
	theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen,|,image,insertimage",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "bottom",
	theme_advanced_resizing : false,

	// Drop lists for link/image/media/template dialogs
	//template_external_list_url : "js/template_list.js",
	external_link_list_url : "js/link_list.php",
	//media_external_list_url : "js/media_list.js",
	//external_image_list_url : "js/image_list.php"
	
});
</script>
		
		<?php if ($error_message) { ?>
		  <div class="message red">
			<h4>Error!</h4>
			<?=$error_message?>
		  </div>
		<?php } ?>
		
		
		
            <div class="topbuttons">
            	<a href="course_edit.php?course_id=<?=$course_id?>#dates">Back to course edit</a>
            </div>
            
            <h3>Manage dates for course <?=sth($row_course['name'])?><br>
            School: <?=$_GET['school_name']?></h3>
            
            <table id="listing" width="100%" cellpadding="0" cellspacing="0">
                <thead>
                    <tr>
                        <td>Date</td>
                        <td>Duration</td>
                        <td>Tuition</td>
                        <td>Options</td>
                    </tr>
                </thead>
                <tbody>
                <?php
          			
				$sql = 'SELECT
						  *
						FROM
						  course_dates
						WHERE
						  course_id = '.$course_id.'
						ORDER BY
							start_date, duration
					';
				$result = query($sql);
					
                  $i=1;
                  while ($row = mysql_fetch_object($result)) {				  
                ?>
                    <tr class="<?=(($i%2)?"none":"zebra")?>">
                        
                        <td><?=$row->start_date?></td>
                        <td><?=$row->duration?> weeks</td> 
                        <td>$<?=$row->tuition?></td>
                                                
                        <td class="icons">
                                                    
                            <a onClick="if (window.confirm('Do you really want to delete this date?\n<?=sth($row->start_date)?>')) { document.location='course_date_del.php?course_date_id=<?=$row->course_date_id?>&course_id=<?=$course_id?>'; return false; }" class="delete" title="Delete">Delete</a>
                            
                        </td>
                        
                    </tr>
                        
                 <?php
                      $i++;    
                    } 
                 ?>	
                      
                </tbody>
            </table>
            
            <form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
            <input type="hidden" name="is_submitted" value="1" />
            <input type="hidden" name="course_id" value="<?=$course_id?>" />
            
            
            
			<dl class="clearfix">
            
            <h3>Add new date</h3>
            
            <dd>
            Start Date
            <script>DateInput('start_date',true,'YYYY-MM-DD',
    		'<?=(($_POST['start_date'])?date('Y-m-d',strtotime($_POST['start_date'])):date('Y-m-d'))?>')</script>
            <br>

            Duration
            <input class="inputbox" style="width:120px;" name="duration" type="text" value="<?=$_POST['duration']?>" maxlength="20" />
            weeks
            <br>
            Tuition $
            <input class="inputbox" style="width:120px;" id="lat" name="tuition" type="text" value="<?=$_POST['tuition']?>" maxlength="20" />
            
                    
            <input style="float:right" type="submit" name="submit" id="submit" value="Add">
			</dd>
            </dl>
			</form>
            
<?php
	foot_admin(); 
?>